//
//  CustomNavView.m
//  Kugou-OC
//
//  Created by BH on 16/3/31.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "CustomNavView.h"
#import <SDAutoLayout/UIView+SDAutoLayout.h>
#import "GlobalDefine.h"

@interface CustomNavView (){
    NSInteger selectTag;
}

@end

@implementation CustomNavView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //设置背景色
        self.backgroundColor = COLOR(44, 120, 218, 1);
        
        //初始话subview
        self.leftItem = [UIButton new];
        self.middleItem = [UIView new];
        self.rightItem = [UIButton new];
        
        //左侧分割线
        UIImageView *leftLine = [UIImageView new];
        leftLine.image = [UIImage imageNamed:@"line_short_vertical"];

        //右侧分割线
        UIImageView *rightLine = [UIImageView new];
        rightLine.image = [UIImage imageNamed:@"line_short_vertical"];

        //添加subview
        [self sd_addSubviews:@[self.leftItem,leftLine,self.middleItem,rightLine,self.rightItem]];
        
        //设置frame
        self.leftItem.sd_layout
        .leftSpaceToView(self,0)
        .topSpaceToView(self,10)
        .widthIs(70)
        .heightIs(70);
        
        leftLine.sd_layout
        .leftSpaceToView(self.leftItem,0)
        .topSpaceToView(self,5)
        .widthIs(1)
        .heightIs(70);
        
        self.middleItem.sd_layout
        .leftSpaceToView(leftLine,0)
        .topSpaceToView(self,10)
        .widthIs(self.width-self.leftItem.width*2-2)
        .heightIs(self.height-10);

        rightLine.sd_layout
        .leftSpaceToView(self.middleItem,0)
        .topEqualToView(leftLine)
        .widthIs(leftLine.width)
        .heightIs(leftLine.height);
        
        self.rightItem.sd_layout
        .leftSpaceToView(self.middleItem,0)
        .topEqualToView(self.leftItem)
        .widthIs(self.leftItem.width)
        .heightIs(self.leftItem.height);
        
        //设置中间view
        [self setMiddleItemSubView];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //左侧按钮
    [self.leftItem setImage:[UIImage imageNamed:@"home_nav_left_logo"] forState:UIControlStateNormal];
    
    //右侧按钮
    [self.rightItem setImage:[UIImage imageNamed:@"home_drawer"] forState:UIControlStateNormal];
    [self.rightItem setImage:[UIImage imageNamed:@"home_drawer_light"] forState:UIControlStateSelected];
}

/**
 *  middleview 听、唱、看
 */
-(void)setMiddleItemSubView
{
    NSArray *normalImageArr = @[@"home_nav_listen",@"home_nav_look",@"home_nav_song"];
    NSArray *highlightImageArr = @[@"home_nav_listen_highlighted",@"home_nav_look_highlighted",@"home_nav_song_highlighted"];
    UIButton *btn = nil;
    for (int i=0; i<3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 100+i;
        [button setImage:[UIImage imageNamed:normalImageArr[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:highlightImageArr[i]] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(navButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.middleItem addSubview:button];
        
        if (btn) {
            button.sd_layout.leftSpaceToView(btn,0).topSpaceToView(self.middleItem,0).widthIs(btn.width).heightIs(btn.height);
        }else{
            button.sd_layout.leftSpaceToView(self.middleItem,0).topSpaceToView(self.middleItem,0).widthIs(self.middleItem.width/3).heightIs(self.middleItem.height);
        }
        
        if (i==0) {
            button.selected = YES;
            selectTag = button.tag;
        }
        
        btn = button;
    }
}

-(void)navButtonClick:(UIButton *)button{
    //取消之前选中项
    if (button.tag != selectTag) {
        UIButton *lastBtn = [self viewWithTag:selectTag];
        lastBtn.selected = NO;
        selectTag = button.tag;
    }
    //选中当前项
    if (!button.selected) {
        button.selected = YES;
    }
    
    if ([self.delegate respondsToSelector:@selector(navBtnClick:)]) {
        [self.delegate navBtnClick:button.tag];
    }
    
}

@end
